package com.jason.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Material;
import com.jason.system.entity.SolveOrder;
import com.jason.system.mapper.MaterialMapper;
import com.jason.system.mapper.OrderMapper;
import com.jason.system.mapper.SolveOrderMapper;
import com.jason.system.mapper.StudentMapper;
import com.jason.system.service.SolveOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/3/26 20:37
 */
@Service
public class SolveOrderServiceImpl implements SolveOrderService {

  @Resource
  SolveOrderMapper solveOrderMapper;

  @Resource
  OrderMapper orderMapper;

  @Resource
  MaterialMapper materialMapper;

  @Resource
  StudentMapper studentMapper;

  @Override
  public List<SolveOrder> getAllSolveOrderByTime(Date startTime, Date endTime) {
    return solveOrderMapper.getSolveOrdersByTime(startTime,endTime);
  }

  @Override
  public Pager<SolveOrder> searchSolveOrders(Integer page, Integer size, Integer sId) {
    PageHelper.startPage(page,size);
    List<SolveOrder> solveOrders = solveOrderMapper.getSolveOrdersBySId(sId);
    for(SolveOrder so : solveOrders){
      List<Integer> maintainers = Arrays.stream(so.getMaintainer().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      so.setMaintainers(studentMapper.getStudentByList(maintainers));
    }
    PageInfo<SolveOrder> solveOrderPageInfo = new PageInfo<>(solveOrders);
    return new Pager<>(solveOrderPageInfo);
  }

  @Override
  public SolveOrder getSolveOrderById(Integer soId) {
    if (soId==null){
      return null;
    }
    return solveOrderMapper.getSolveOrderById(soId);
  }

  @Override
  public Pager<SolveOrder> getSolveOrders(Integer page, Integer size) {
    PageHelper.startPage(page,size);
    List<SolveOrder> solveOrders = solveOrderMapper.getSolveOrders();
    for(SolveOrder so : solveOrders){
      List<Integer> maintainers = Arrays.stream(so.getMaintainer().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      so.setMaintainers(studentMapper.getStudentByList(maintainers));
    }
    PageInfo<SolveOrder> solveOrderPageInfo = new PageInfo<>(solveOrders);
    return new Pager<>(solveOrderPageInfo);
  }

  @Override
  public void insertSolveOrder(SolveOrder solveOrder) {
    materialMapper.insertMaterial(solveOrder.getMaterial());
    if(solveOrder.getStatus()==1){
      orderMapper.updateOrderStatus(solveOrder.getOId(),1);
    }
    solveOrder.setMId(solveOrder.getMaterial().getMId());
    solveOrderMapper.insertSolveOrder(solveOrder);
  }

  @Override
  public void updateSolveOrder(SolveOrder solveOrder) {
//    orderMapper.updateOrder(solveOrder.getOrder());
    materialMapper.updateMaterial(solveOrder.getMaterial());
    solveOrderMapper.updateSolveOrder(solveOrder);
  }

  @Override
  public void updateSolveOrderStatus(Integer soId, Integer status) {
    solveOrderMapper.updateSolveOrderStatus(soId,status);
  }

  @Override
  public boolean deleteSolveOrderById(Integer soId) {
    if (soId==null){
      return false;
    }else {
      SolveOrder solveOrder = solveOrderMapper.getSolveOrderById(soId);
      materialMapper.deleteMaterial(solveOrder.getMId());
      solveOrderMapper.deleteSolveOrderById(soId);
      return true;
    }
  }

  @Override
  public boolean deleteSolveOrders(String soIds) {
    if("".equals(soIds)||soIds==null){
      return false;
    }else {
      List<Integer> soId = Arrays.stream(soIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      for (Integer s :soId){
        SolveOrder solveOrder = solveOrderMapper.getSolveOrderById(s);
        materialMapper.deleteMaterial(solveOrder.getMId());
      }
      solveOrderMapper.deleteSolveOrders(soId);
      return true;
    }
  }
}
